package behavior.iterator;

import java.util.ArrayList;
import java.util.List;

/**
 * @author dali
 * @date 2021-07-26 14:26
 * @Description
 */

public class ConcreteIterable implements MyIterable<Student> {
    private List<Student> list = new ArrayList<>();
    public ConcreteIterable() {
        list.add(new Student(1,"小王"));
        list.add(new Student(2,"小李"));
        list.add(new Student(3,"小张"));
    }

    @Override
    public MyIterator iterator() {
        return new ConcreteIterator();
    }

    public boolean addStudent(Student s) {
       return list.add(s);
    }

    public boolean removeStudent(Student s) {
        return list.remove(s);
    }

    private class ConcreteIterator implements MyIterator<Student> {

        private int index = 0;

        @Override
        public boolean hasNext() {
            return index < list.size();
        }

        @Override
        public Student next() {
            return list.get(index++);
        }
    }
}


